package com.da_ping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 */
@Controller
@RequestMapping("/da_ping/common_da_ping")
public class ControllerDaPing extends BaseController {

	@Autowired
	private Dao dao;

	/**
	 */
	@GetMapping("/operate_get")
	@ResponseBody
	public void getZwl(HttpServletResponse response, HttpServletRequest request) {
		try {
			String sql = request.getParameter("sql");
			if (sql.contains("delete") || sql.contains("drop")) {
				if (!sql.contains("select") || !sql.contains("insert") || !sql.contains("update")) {
					fail(response);
				}
			}
			if (sql.contains("select")) {
				outJson(response, dao.list(sql).toString());
			}
			if (sql.contains("update")) {
				if (dao.execute(sql)) {
					success(response);
				} else {
					fail(response);
				}
			}
			if (sql.contains("insert")) {
				if (dao.execute(sql)) {
					success(response);
				} else {
					fail(response);
				}
			}

		} catch (Exception e) {
			fail(response);
		}
	}

	boolean isStop = false;// 线程是否停止

	/**
	 */
	@GetMapping("/begin")
	@ResponseBody
	public void begin(HttpServletResponse response, HttpServletRequest request) {
		try {
			isStop = false;
			String sql = "";
			List rtn = new ArrayList();

			rtn = dao.list("select * from da_ping_option");

			if (rtn.size() > 0) {
				JSONArray rtn_ja = Util.list_to_JSONArray(rtn);
				for (int i = 0; i < rtn_ja.size(); i++) {
					JSONObject obj = rtn_ja.getJSONObject(i);
					int shi_fou_qi_yong = obj.getInt("shi_fou_qi_yong");
					if (shi_fou_qi_yong == 1) {
						new Thread(new Runnable() {
							@Override
							public void run() {
								try {
									String edit_id = obj.getString("edit_id");
									int kai_shi_zhi = obj.getInt("kai_shi_zhi");
									int jie_shu_zhi = obj.getInt("jie_shu_zhi");
									int jian_ge_zhi = obj.getInt("jian_ge_zhi");
									int dang_qian_zhi = obj.getInt("dang_qian_zhi");

									while (dang_qian_zhi < jie_shu_zhi) {
										if (isStop) {
											break;
										}
										dang_qian_zhi += jian_ge_zhi;
										if (dang_qian_zhi >= jie_shu_zhi) {
											dang_qian_zhi = jie_shu_zhi;
										}
										HashMap map = new HashMap();
										dao.execute("update da_ping_option set dang_qian_zhi=" + dang_qian_zhi
												+ " where edit_id='" + edit_id + "'");
										Thread.sleep(1000);
									}

								} catch (Exception e) {
									fail(response);
								}
							}

						}).start();
					}

				}
			}
			success(response);
		} catch (Exception e) {
			fail(response);
		}
	}

	/**
	 */
	@GetMapping("/stop")
	@ResponseBody
	public void stop(HttpServletResponse response, HttpServletRequest request) {
		try {
			isStop = true;
			success(response);
		} catch (Exception e) {
			fail(response);
		}
	}

	/**
	 */
	@GetMapping("/reset")
	@ResponseBody
	public void reset(HttpServletResponse response, HttpServletRequest request) {
		try {
			isStop = true;
			if (dao.execute("update da_ping_option set dang_qian_zhi=kai_shi_zhi")) {
				success(response);
			}
		} catch (Exception e) {
			fail(response);
		}
	}

}